package com.pn.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;


public class JwtUtil {



    private static final String KEY = "itheima";
    private static final int ExpireTime = 2880000;

    /*@Value("${warehouse.expire-time}")
    private String expireTime;*/

    //生成token
    public static String createJWT(Map<String,Object> claims){

        return JWT.create()
                .withClaim("claims",claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + ExpireTime))
                .sign(Algorithm.HMAC256(KEY));
    }

    //解析token
    public static Map<String,Object> parseToken(String token){
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
